package com.binge.excel;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * ClassName:ExcelTest
 * Package:com.binge.excel
 * Description:
 *
 * @Author @Bin
 * @Date 2025/2/11-17:24
 * @Version: v1.0
 */
public class ExcelTest {

    @Test
    public void testParseJson() throws IOException {

        // Iterate over JSON objects
        //json 格式如下
        //[
        //   {
        //      "display": "默认参照型号",
        //      "fetchDeviceDefaultValues": {},
        //      "subtype": 0
        //   },
        //   {
        //      "display": "H323",
        //      "fetchDeviceDefaultValues": {
        //         "mediaConfig": {
        //            "pvrxFrameRate265": "15",
        //            "pvrxFrameRate265V2": "15",
        //            "pvrxResolution265": "320_180",
        //            "pvrxResolution265V2": "320_180",
        //            "pvtxEncoderGroups265": "",
        //            "pvtxEncoderGroups265V2": "[]",
        //            "pvtxFrameRate265": "",
        //            "pvtxFrameRate265V2": "0",
        //            "pvtxResolution265": "",
        //            "pvtxResolution265V2": ""
        //         },
        //         "RemoteSDKConfig": {
        //            "bifRetransmitTimer": "[{\"type\":1,\"interval\":8000,\"count\":4},{\"type\":2,\"interval\":8000,\"count\":4},{\"type\":3,\"interval\":8000,\"count\":4},{\"type\":4,\"interval\":8000,\"count\":4}]",
        //            "enableRetransmit": "false",
        //            "ResolutionFrameratePriorityMode": "0"
        //         },
        //         "UIDisplayCustomization": {
        //            "showLive": "false"
        //         }
        //      },
        //      "subtype": 6
        //   }


        //遍历每个对象
        //每个对象创建一个sheet页，sheet页的名字是对象中属性display-subtype
        //每个sheet页的第一行是header，包含三列：configname,configvalue,group
        //每个sheet页的第二行开始是数据，每条数据是对象中的configurations数组中的一条
        //每个数据行包含三列：configname,configvalue,fieldName
        //configname 为configurations数组中对象的key，configvalue为configurations数组中对象的value，group 为外在包装层，例如UIDisplayCustomization，RemoteSDKConfig
        //"UIDisplayCustomization": {
        //                 "showLive": "false"
        //              } 为例
        //结果为 showLive,false,UIDisplayCustomization






        // Read JSON file
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(new File("/Users/bin/Desktop/a.json"));

        // Create Excel workbook
        Workbook workbook = new XSSFWorkbook();

        // Iterate over JSON objects
        Iterator<JsonNode> elements = rootNode.elements();
        while (elements.hasNext()) {
            JsonNode node = elements.next();

            // Get display and subtype as sheet name
            String sheetName = node.get("display").asText() + "-" + node.get("subtype").asText();
            Sheet sheet = workbook.createSheet(sheetName);

            // Create header row
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("configname");
            headerRow.createCell(1).setCellValue("configvalue");
            headerRow.createCell(2).setCellValue("group");

            // Fill table content
            int rowIndex = 1;
            JsonNode fetchDeviceDefaultValues = node.get("fetchDeviceDefaultValues");
            Iterator<Map.Entry<String, JsonNode>> fields = fetchDeviceDefaultValues.fields();
            while (fields.hasNext()) {
                Map.Entry<String, JsonNode> field = fields.next();
                String groupName = field.getKey();
                JsonNode groupNode = field.getValue();

                Iterator<Map.Entry<String, JsonNode>> groupFields = groupNode.fields();
                while (groupFields.hasNext()) {
                    Map.Entry<String, JsonNode> groupField = groupFields.next();
                    Row row = sheet.createRow(rowIndex++);
                    row.createCell(0).setCellValue(groupField.getKey());
                    row.createCell(1).setCellValue(groupField.getValue().asText());
                    row.createCell(2).setCellValue(groupName);
                }
            }
        }

        // Write workbook to file
        try (FileOutputStream fileOut = new FileOutputStream("output.xlsx")) {
            workbook.write(fileOut);
        }

        // Close workbook
        workbook.close();
    }
    }

